﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using PNProject.EF;
using PNProject.Models;

namespace PNProject.Controllers
{
    public partial class AdminMapController : Controller
    {
        public ActionResult Index()
        {
            var locations = StaticData.locationMapsRepo.GetAll();
            var models = new List<LocationModel>();
            var str = string.Empty;
            if (locations.Any())
            {
                var i = 0;
                foreach (var item in locations)
                {
                    var local = new LocationModel();
                    local.Address = item.Address;
                    local.LocaitonId = item.LocationId;
                    local.Lat = item.Lat.Value;
                    local.Lng = item.Lng.Value;
                    models.Add(local);
                    if (i == locations.Count()-1)
                    {
                        str += item.LocationId;    
                    }
                    else
                    {
                        str += item.LocationId + ",";
                    }
                    i++;
                }
                ViewBag.Ids = str;
            }
            return View(models);
        }
        public ActionResult Create()
        {
            return View(new LocationModel());
        }

        [HttpPost]
        public ActionResult Create(LocationModel model,FormCollection form)
        {
            var localMap = new LocationMap();
            localMap.LocationId = Guid.NewGuid();
            localMap.Address = model.Address;
            var lat = form["lat"];
            var lng = form["lng"];
            localMap.Lat = decimal.Parse(lat);
            localMap.Lng = decimal.Parse(lng);
            StaticData.locationMapsRepo.Save(null, localMap);
            return RedirectToAction("Index");
        }
        public ActionResult Edit(string id)
        {
            try
            {
                var newid = Guid.Parse(id);
                var location = StaticData.locationMapsRepo.GetOne(a => a.LocationId == newid);
                var model = new LocationModel();
                model.Address = location.Address;
                model.LocaitonId = location.LocationId;
                model.Lat = location.Lat.Value;
                model.Lng = location.Lng.Value;
                return View(model);
            }
            catch (Exception e)
            {
                return RedirectToAction("Index");
            }
           
            
            return View();
        }

        [HttpPost]
        public ActionResult Edit(LocationModel model)
        {
            Guid newid;
            try
            {
                //newid = model.LocaitonId;
            }
            catch (Exception e)
            {
                return RedirectToAction("Index");
            }
            
            return RedirectToAction("Index");
        }
    }
}
